package com.softer.chengxin.home.dao.provider;

import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.common.enums.NumberEnum;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;

/**
 * @author Gong Zhiyang
 * @date 2018/12/25
 */
public class HomeDaoProvider {
    /**
     * 查询企业数量
     *
     * @param map 参数
     * @return SQL
     */
    public String getOnlineCorpNum(HashMap<String, Object> map) {
        StringBuffer sql = new StringBuffer(20);
        String area = (String) map.get("area");
        String online = (String) map.get("online");
        sql.append("SELECT COUNT(*)FROM company_base_info");
        sql.append(" WHERE street in (").append(area).append(")");
        commonSql(sql, online);
        return sql.toString();
    }

    /**
     * 查询企业待办事项
     * @param map 参数
     * @return SQL
     */
    public String findCountByState(HashMap<String, Object> map) {
        StringBuffer sql = new StringBuffer(20);
        String ids = (String) map.get("ids");
        String state = (String) map.get("state");
        String provinceCompany = (String) map.get("provinceCompany");
        sql.append(" select COUNT(*) from company_base_info ");
        sql.append(" WHERE street in (").append(ids).append(")" );
        sql.append(" AND state in (").append(state).append(")" );
        if (StringUtils.isNotBlank(provinceCompany)) {
            //1省内   0省外
            if(String.valueOf(NumberEnum.ONE.getNum()).equals(provinceCompany)){
                sql.append(" and province_company = #{provinceCompany} ");
            }else if(String.valueOf(NumberEnum.ZERO.getNum()).equals(provinceCompany)){
                sql.append(" OR (state IN (1) AND  province_company = #{provinceCompany}) ");
            }
        }
        return sql.toString();
    }

    /**
     * 查询在线企业列表
     *
     * @param map 参数
     * @return SQL
     */
    public String getOnlineCorpList(HashMap<String, Object> map) {
        StringBuffer sql = new StringBuffer(20);
        String area = (String) map.get("area");
        String online = (String) map.get("online");
        Page page = (Page) map.get("page");
        sql.append("SELECT id,qy_no as qyNo,`name`,`online` FROM company_base_info");
        sql.append(" WHERE street in (").append(area).append(")");
        commonSql(sql, online);
        sql.append(" LIMIT ");
        sql.append(page.getBeginIndex()).append(",").append(page.getLimit());
        return sql.toString();
    }

    /**
     * 公共查询字段
     *
     * @param sql    sql
     * @param online 在线（0否，1是）
     */
    public void commonSql(StringBuffer sql, String online) {
        if (StringUtils.isNotBlank(online)) {
            sql.append("  and `online`=#{online}");
        }
    }
}
